/**
 * @author ciwyaitd
 * @author levy
 *    重构显示数字/菜单权限控制
 * @date   16/1/7
 * @description []
 */

import TipNum from "../components/TipNum.js";
import {health} from "../const/offices";
import {prefix} from "../const/prefix";

(function (lf, r) {
    var RC = r.createClass,
        doAction = lf.bridgeJs.doAction,
        api = health.api,
        apiUrl = prefix.oldpeople,
        ajax = lf.utils.ajax,
        dataId,
        log = lf.utils.log,
    //TODO 这些icon以后会增加，可能会重写.
        item_list = [
            //{
            //   name: '报事登记',
            //	icon: 'report',
            //   bgclass: 'purple-bg',
            //	url: 'report.html'
            //},
            //{
            //	bgclass: 'blue-bg',
            //	icon: 'security',
            //	name: '报事处理',
            //	url:"report-list.html"
            //},
            //{
            //	bgclass: 'green-bg',
            //	icon: 'sign_for',
            //	name: '工单签收',
            //	url: 'sign-for.html'
            //
            //},
            //{
            //	bgclass: 'green-bg',
            //	icon: 'rush',
            //	name: '抢单',
            //	url: 'rush.html'
            //},
            //{
            //	bgclass: 'blue-bg',
            //	icon: 'task_processing',
            //	name: '工单处理',
            //	url: 'task-processing.html'
            //},
            //{
            //	bgclass: 'blue-bg',
            //	icon: 'absence_apply',
            //	name: '审批',
            //	url: 'approval.html'
            //},
            //{
            //	bgclass: 'blue-bg',
            //	icon: 'daily_work',
            //	name: '通知',
            //	url: 'notice.html'
            //},
            {
                bgclass: 'orange-bg',
                icon: 'work_shift',
                name: '交接班',
                //url: 'http://42.96.156.156/html/event.html',
            },
            {
                bgclass: 'red-bg',
                icon: 'food_ordering',
                name: '订餐服务',
            },
            {
                bgclass: 'red-bg',
                icon: 'meter_checking',
                name: '抄表'
            },
            {
                bgclass: 'purple-bg',
                icon: 'sign_in_night_shift',
                name: '夜班签到',
                //url: 'sign_in_night.html'
            },
            {
                bgclass: 'purple-bg',
                icon: 'sign_in_outside',
                name: '外出签到',
                //url: 'outside.html'
            },
            {
                bgclass: 'blue-bg',
                icon: 'attendance',
                name: '考勤'
            },
            {
                bgclass: 'red-bg',
                icon: 'decoration_detail',
                name: '装修进度',
            },
            {
                bgclass: 'white-bg',
                icon: 'zen',
                name: '禅道',//oemCode = lf_property 才能看见
                url: 'http://www.liefengtech.com:82/zentao/user-login-L3plbnRhby8=.mhtml',
            },
            {
                bgclass: 'white-bg',
                icon: 'application_add',
                name: '添加应用'
            }
        ];
    
    
    var BtnIcon = RC({
        getInitialState: function () {
            return {
                useState: false, //为false时,num不启作用.
                num: ''
            }
        },
        render: function () {
            var dt = this.props.data,
                count = this.props.count,
                num = this.state.useState ? this.state.num : this.setupTaskCount(dt.url, count),
                filterClass = '';
            
            if (!dt.url) {
                filterClass = ' filter-blur'
            }
            //生产禅道,列丰才能见
            if (dt.icon == 'zen' && lf.userInfo.oemCode != 'lf_property')
                filterClass += ' ele-hide';
            
            return (
                <div onTouchEnd={this.clickFn} className={"item" + filterClass}>
                    <div className="circle">
                        <a ref="icon" className={dt.bgclass + " " + dt.icon}>
                            <TipNum num={num}/>
                        </a>
                    </div>
                    <p>{dt.name}</p>
                </div>
            )
        },
        /**
         * 更新了工单的数字,则别的数字也要更新
         */
        componentWillReceiveProps() {
            var url = this.props.data.url;
            //不得已而为之,因为获取数据的接口不是统一一个
            this.getApprovalNum(url);

            this.getReportListNum(url);

            this.getNoticeNum(url);

            this.getDailyWorkNum(url);

            this.getConsultNum(url);
        },
        componentDidMount: function () {

        },
        //报事处理
        getReportListNum(orl) {
            var me = this;
            if (orl && orl.indexOf('report-list-oa') > -1) {
                console.log('报事处理')
                ajax({
                    url:apiUrl + api.list,
                    type:'get',
                    data: {
                        page: 1,
                        pageSize: 1,
                        staffId: dataId.id,
                    },
                    headers: {
                        openId: dataId.openId,
                    },
                    success(data) {
                        me.setNum(data.maxCount);
                    }
                });
            }
        },
        //通知
        getNoticeNum(orl) {
            var me =this;
            if (orl && orl.indexOf('notice-oa') > -1) {
                ajax({
                    url:apiUrl + api.num,
                    type:'get',
                    data: {
                        staffId: dataId.id,
                        deptId: lf.userInfo.departmentId,
                    },
                    headers: {
                        openId: dataId.openId,
                    },
                    success(data) {
                        let num = 0;

                        data.forEach(item => {
                            num += item.number;
                        });

                        me.setNum(num);
                    }
                });
            }
        },
        //日常任务
        getDailyWorkNum(orl,data) {
            var me = this;
            if (orl && orl.indexOf('daily-work-oa') > -1) {
                ajax({
                    url: apiUrl + api.taskNum,
                    type: 'post',
                    data: {
                        staffId: dataId.id,
                    },
                    headers: {
                        openId: dataId.openId,
                    },
                    success(data) {
                        let num = 0;
                        for (let i in data) {
                            num += data[i];
                        }
                        me.setNum(num);

                    }
                });
            }
        },
        //报事咨询
        getConsultNum(orl,data) {
            var me = this;
            if (orl && orl.indexOf('report-advisory-oa') > -1) {
                ajax({
                    url: apiUrl + api.amount,
                    type: 'get',
                    data: {
                        staffId: dataId.id,
                    },
                    headers: {
                        openId: dataId.openId,
                    },
                    success(data) {
                        let num = 0;
                        num = parseInt(data.notCheckAmount) + parseInt(data.notReplyAmount);
                        me.setNum(num);
                    }
                });
            }
        },
        /**
         * 审批数字
         */
        getApprovalNum(orl,data) {
            var me = this;
            if (orl && orl.indexOf('approval-oa') > -1) {
                ajax({
                    url:apiUrl + api.getCount,
                    type: 'get',
                    data: {
                        staffId: dataId.id
                    },
                    headers: {
                        openId:dataId.openId,
                    },
                    success(data) {
                        me.setNum(data.workFlowWaitCount);
                    }
            });
            }
        },
        setNum(num) {
            this.setState({
                useState: true,
                num: num
            });
        },
        /**
         * 设置工单统一接口返回的数字
         * @param url
         * @param count
         * @returns {*}
         */
        setupTaskCount: function (url, count) {
            //log(count)
            if (!url || !count) return;

            if (url.indexOf('task-processing-oa') > -1)
                return count.waitDealCount;

            if (url.indexOf('rush-oa') > -1)
                return count.grabCount;

            if (url.indexOf('sign-for-oa') > -1)
                return count.waitSignCount;
        },
        clickFn: function () {
            var dt = this.props.data;
            switch (dt.url) {
                case 'sign-in-night.html':
                    //夜班签到界面
                    doAction(dt.url, dt.name, 'signNight');
                    break;
                case 'outside.html':
                    //外勤签到界面
                    doAction(dt.url, dt.name, 'signOutside');
                    break;
                case 'report-oa.html':
                    //报事界面
                    doAction(dt.url, dt.name, 'report');
                    break;
                default:
                    //一般页面,
                    doAction(dt.url + `?menuId=${dt.id}`, dt.name);
                    break;
            }
        }
    });

    var IndexBody = RC({
        getInitialState: function () {
            return {
                items: [],
                user: '',
                taskCount: {} //工单相关数据
            };
        },
        componentWillMount: function () {
            this.getTaskCount();

            lf.appJs.refresh(() => this.getTaskCount());
        },

        //抢单/签收/工单列表头部
        getTaskCount: function () {
            var me = this;
            ajax({
                url: apiUrl + api.listHeader,
                data: {
                    actor: dataId.id,
                },
                headers: {
                    openId: dataId.openId,
                },
                success: function (resp) {
                    me.setState({taskCount: resp.data});
                    lf.appJs.doLoading(false);
                }
            });
        },
        initIcon: function () {
            var item2DArray = lf.utils.to2DArray(this.props.itemList.dataList || [], 4);
            return item2DArray.map((array, i) => {
                return (
                    <div key={'array' + i} className="item-row">
                        {
                            array.map(item => {
                                return <BtnIcon key={item.id} data={item} count={this.state.taskCount}/>
                            })
                        }
                    </div>
                );
            });
        },

        render: function () {
            if (!this.props.itemList.dataList.length) //数据为空
                return <div style={{textAlign: 'center', paddingTop: '1rem'}}>请联系管理员授权</div>

            else
                return (
                    <div className="icon-con">
                        {this.initIcon()}
                    </div>
                )
        }
    });

    lf.bridgeJs.getUser(function (user) {
        console.log(user)

        lf.appJs.doLoading(true);
        var me = this;
        ajax({
            url: apiUrl + api.getGlobalId,
            type: 'post',
            data: {
                custGlobalId: user.globalId||user.custLoginVo.globalId,//user.id,'C1606281657108af41b8e068927' ||
            },
            success: function (data) {
                dataId = data.data;
                if(data.code==200)
                ajax({
                    url: apiUrl + api.menuList,
                    type: 'post',
                    data: {
                        staffId:dataId.id, //员工id
                        sysMenuId: 0,
                    },
                    headers: {
                        openId: dataId.openId,
                    },
                    success: function (res) {
                        r.render(
                            <IndexBody itemList={res} user={user}/>,
                            document.getElementById('root'), function () {

                            }
                        )
                    }
                });
            }
        });
    });

})(liefeng, React);
